<template>
	<view class="search-page">
		<u-sticky bgColor="#fff" style="width: 750rpx;">
			<view class="search-tit">
				<view class="ipt-box">
					<image src="../static/search.png" mode="aspectFill"></image>
					<input type="text" placeholder="请输入您要查找的车辆" placeholder-style="color: #CDCDCD"
						v-model="searchName" />
				</view>
				<view class="btn" @click="handleSearch">搜索</view>
			</view>
			<view class="item-common header-wrapper" style="margin-bottom: 15rpx;">
				<view>序号</view>
				<view>选择</view>
				<view>状态</view>
				<view>车牌</view>
			</view>
			<view class="" style="width: 100%;height: 1px;background-color: #ddd;margin-bottom: 8rpx;">

			</view>
		</u-sticky>
		<view class="item-common border-wrapper" v-for="(item, index) in lists" :key="item.id"
			@click="handleThis(item)">
			<view>{{index + 1}}</view>
			<view>
				<image src="../static/radius_a.png" mode="aspectFill" class="radius" v-if="item.id == car.id">
				</image>
				<image src="../static/radius.png" mode="aspectFill" class="radius" v-else></image>
			</view>
			<view>
				<image :src="item.positionImg" mode="aspectFit" class="radius"></image>
			</view>
			<view>{{item.operatorName}}</view>
		</view>
		<view class="" style="height: 200rpx;">

		</view>

		<btnBottom text="确 定" @onBtnClick="onBtnClick"></btnBottom>
	</view>
</template>

<script>
	import btnBottom from '../components/btnBottom.vue';
	import hwOperatorLocationApi from '@/api/hwOperator/hwOperatorLocationApi'

	export default {
		components: {
			btnBottom
		},
		props: {
			top: String,
			topTwo: String
		},
		data() {
			return {
				searchName: '',
				lists: [],
				car: {},
				operatorList: [],
				noSelect: true,
			}
		},
		mounted() {
			this.getVehicleList()
		},
		methods: {
			handleSearch() {
				this.getFilterVehicleList()
			},
			handleThis(msg) {
				this.car = msg
			},
			onBtnClick() {
				if (this.noSelect) {
					this.noSelect = false
					const item = this.car
					let operatorHistory = uni.getStorageSync('operatorHistory')
					if (operatorHistory) {
						const result = operatorHistory.some(car => car.id === item.id)
						if (!result) {
							operatorHistory.push(item)
							uni.setStorageSync('operatorHistory', operatorHistory)
						}
					} else {
						uni.setStorageSync('operatorHistory', [item])
					}
					this.$store.commit('SET_OPERATOR_INFO', item)
					uni.navigateBack({

					})
				}
			},
			getVehicleList() {
				let that = this
				uni.showLoading({
					title: '加载中',
					mask: true
				})

				hwOperatorLocationApi.hwOperatorRealTime({

				}).then((res) => {
					if (res.code == 200) {
						var list = res.data.operatorList
						that.operatorList.splice(0, that.operatorList.length)
						for (var i in list) {
							var item = list[i]
							that.operatorList.push(item)
						}
					}
				})
			},

			getFilterVehicleList() {
				let that = this
				if (this.operatorList && this.operatorList.length > 0) {
					this.filterVehicleNum()
				} else {
					uni.showLoading({
						title: '加载中',
						mask: true
					})

					hwOperatorLocationApi.hwOperatorRealTime({

					}).then((res) => {
						if (res.code == 200) {
							var list = res.data.operatorList
							that.operatorList.splice(0, that.operatorList.length)
							for (var i in list) {
								var item = list[i]
								that.operatorList.push(item)
							}
							if (that.searchName && that.searchName.length > 0) {
								that.filterVehicleNum()
							}
						}
					})
				}
			},
			filterVehicleNum() {
				let that = this
				that.lists.splice(0, that.lists.length)
				for (var i in this.operatorList) {
					var obj = this.operatorList[i]
					if (obj.operatorName.toLowerCase().includes(that.searchName.toLowerCase())) {
						that.lists.push(obj)
					}
				}
			}
		}
	}
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped>
	.search-page {
		padding: 0 0rpx;
	}

	.search-tit {
		width: 702rpx;
		height: 98rpx;
		margin-left: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #333333;
		background: #fff;
		font-size: 28rpx;
	}

	.ipt-box {
		display: flex;
		align-items: center;
		flex: 1;
		height: 70rpx;
		padding-left: 36rpx;
		background: #FFFFFF;
		border: 1px solid #D3D0D0;
		border-radius: 8rpx;
	}

	.ipt-box image {
		width: 36rpx;
		height: 36rpx;
	}

	.ipt-box input {
		flex: 1;
		padding: 0 28rpx;
		font-size: 24rpx;
	}

	.btn {
		font-size: 28rpx;
		color: #3791E9;
		line-height: 70rpx;
		padding-left: 36rpx;
	}

	.border-wrapper {
		border-radius: 8rpx;
		border: 1px solid #E5E5E6;
		margin-bottom: 8rpx;
	}

	.header-wrapper {
		width: 710rpx;
		/* border-bottom: 1px solid #E5E5E6; */
	}

	.item-common {
		width: 710rpx;
		margin-left: 20rpx;
		height: 88rpx;
		background: #FFFFFF;
		font-size: 28rpx;
		display: flex;
		align-items: center;

	}

	.item-common view {
		width: 100rpx;
		height: 30rpx;
		line-height: 30rpx;
		text-align: center;

	}

	.border-wrapper view {
		border-right: 1px solid #E4E4E4;
	}

	.item-common view:nth-child(4),
	.item-common view:nth-child(5) {
		flex: 1;
	}

	.item-common view:nth-child(5) {
		border-right: 0;
	}

	.item-tit {
		position: fixed;
		z-index: 10;
		left: 24rpx;
		width: 702rpx;
	}

	.radius {
		width: 32rpx;
		height: 32rpx;
	}

	.btn-box {
		width: 750rpx;
		padding: 30rpx 0;
		position: fixed;
		left: 0;
		bottom: 0;
		background: #F9F9FA;
		z-index: 10;
	}
</style>